package com.woniuxy.woniuticket.ordermodule.service.serviceimpl;

import com.woniuxy.woniuticket.ordermodule.constant.OrderConstant;
import com.woniuxy.woniuticket.ordermodule.dao.CinemaCommentReplyDAO;
import com.woniuxy.woniuticket.ordermodule.dao.CinemaOderDAO;
import com.woniuxy.woniuticket.ordermodule.dao.MoviesCommentDAO;
import com.woniuxy.woniuticket.ordermodule.service.CommentService;
import com.woniuxy.woniuticket.pojo.CinemaCommentReply;
import com.woniuxy.woniuticket.pojo.CinemaOder;
import com.woniuxy.woniuticket.pojo.MoviesComment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Wu xianbo on 2019/6/1
 */

/*评论service实现类*/
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private MoviesCommentDAO moviesCommentDAO;

    @Resource
    private CinemaCommentReplyDAO cinemaCommentReplyDAO;

    @Resource
    private CinemaOderDAO cinemaOderDAO;

    //添加评论(消费者评论电影,需要获取电影id和用户id)
    @Transactional
    @Override
    public int addComment(MoviesComment moviesComment,String orderNum) {
        moviesCommentDAO.insertSelective(moviesComment); //插入评论
        //修改订单表的状态为已评价
        CinemaOder cinemaOder = new CinemaOder();
        cinemaOder.setOrdernum(orderNum);    //设置订单号
        cinemaOder.setTicketstatus(OrderConstant.HAVE_COMMENT);    //设置评价状态
        cinemaOderDAO.updateByPrimaryKeySelective(cinemaOder);   //修改该用户的订单为已评价
        return 1;
    }

    //根据用户id查询自己的所有评论
    @Override
    public List<MoviesComment> findAllByUserId(Integer userId) {
        List<MoviesComment> moviesComments = moviesCommentDAO.findAllByUserId(userId);
        return moviesComments;
    }

    //商家查询所有评论
    @Override
    public List<MoviesComment> findAllByCinema(Integer cpage, Integer psize,String movieName) {
        return moviesCommentDAO.findAllByCinema(cpage, psize,movieName);
    }

    //根据电影id查询评论以及评论下的回复
    @Override
    public List<MoviesComment> findCommentsByMovieId(Integer movieId) {
        return moviesCommentDAO.findCommentsByMovieId(movieId);
    }

    //回复某一条评论
    @Transactional
    @Override
    public int replyComment(CinemaCommentReply cinemaCommentReply) {
        int row = cinemaCommentReplyDAO.insertSelective(cinemaCommentReply);
        //如果是商家在回复则需要修改评论表的flag为1
        System.out.println("现在的userid"+cinemaCommentReply.getReplyuserId());
        if(cinemaCommentReply.getReplyuserId()==null){
            System.out.println("修改标志了吗");
            //商家回复标志变为1
            MoviesComment moviesComment = new MoviesComment();
            moviesComment.setId(cinemaCommentReply.getCommentId());
            moviesComment.setFlag(1);
            row = moviesCommentDAO.updateByPrimaryKeySelective(moviesComment);
        }
        return row;
    }

    //删除评论
    @Transactional
    @Override
    public int deleteCommentByMovieId(Integer[] commentIds) {
        //先删除评论下的回复
        for (int i = 0; i < commentIds.length; i++) {
            cinemaCommentReplyDAO.deleteReplyByCommentId(commentIds[i]);
            moviesCommentDAO.deleteByPrimaryKey(commentIds[i]);
        }
        return 1;
    }
}
